package com.dbs.zstp.wiki.controller;

import com.dbs.zstp.common.controller.BaseController;
import com.dbs.zstp.common.service.BaseService;
import com.dbs.zstp.wiki.engine.ZhWikiPageProcessor;
import com.dbs.zstp.wiki.model.Wqzb;
import com.dbs.zstp.wiki.service.WqzbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import us.codecraft.webmagic.Spider;

/**
 * wqzb控制类
 *
 * @author DBS-XQ
 * @create 2018-03-26 16:47
 **/
@RestController
@RequestMapping("/wqzbs")
public class WqzbController extends BaseController<Wqzb> {

    @Autowired
    private WqzbService wqzbService;

    @Value("${zhwiki_root_path}")
    private String zhwiki_root_path = "";

    @Override
    public BaseService getService() {
        return wqzbService;
    }

    @GetMapping(value = "/index")
    public String index(String name){
        int count = 0;
        try {
            Spider.create(new ZhWikiPageProcessor(zhwiki_root_path,name))
                    .addUrl("https://zh.wikipedia.org/wiki/Category:"+name)
//                    .addPipeline(new JsonFilePipeline("E:\\webmagic\\"))
//                .addPipeline(new ConsolePipeline())
                    //开启5个线程抓取
                    .thread(10)
                    //启动爬虫
                    .run();
            count = wqzbService.selectAll().size();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return name+"共抓取了"+count+"条数据!";
    }
}
